package com.masterclient.app.modules.order;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.masterclient.app.BaseActivity;
import com.masterclient.app.R;
import com.masterclient.app.modules.firstpager.HomeFragmentPagerAdapter;
import com.eoner.baselibrary.widget.EnhanceTabLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 作者：sean
 * 创建时间：2017/7/26 19:05
 * 功能说明：
 */

public class    CustomerOrderActivity extends BaseActivity {

    @BindView(R.id.rl_back)
    RelativeLayout rlBack;
    @BindView(R.id.tabLayout)
    EnhanceTabLayout tabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.iv_unread)
    ImageView ivUnread;
//    @BindView(R.id.tv_kefu)
//    TextView tvKefu;
    @BindView(R.id.tv_back)
    ImageView tvBack;

    OrderListFragment orderListFragment;
    String[] titles = {"全部","待付款","待发货","待收货","待评价"};
    private List<Fragment> fragments;


    private HomeFragmentPagerAdapter adapter;
//    private RxBus rxBus;


    String action = "refreshUnComemtedFagment";

    private BroadcastReceiver mBoradcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(action)) {
                if(allFagment != null){
                    allFagment.refresh();
                }
                if(unPaidFagment != null){
                    unPaidFagment.refresh();
                }
                if(unDeliveryFagment != null){
                    unDeliveryFagment.refresh();
                }
                if(unReceivedFagment != null){
                    unReceivedFagment.refresh();
                }
                if(unComemtedFagment != null){
                    unComemtedFagment.refresh();
                }
            }
        }
    };

    OrderListFragment allFagment;
    OrderListFragment unPaidFagment;
    OrderListFragment unDeliveryFagment;
    OrderListFragment unReceivedFagment;
    OrderListFragment unComemtedFagment;

    @SuppressLint("CheckResult")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_customer_order);
        ButterKnife.bind(this);

//        tvKefu.setTypeface(iconfont);
//        tvBack.setTypeface(iconfont);
        fragments = new ArrayList<>();
        allFagment = OrderListFragment.getInstance(OrderListFragment.ALL_ORDER,"","user","");
        fragments.add(allFagment);
        unPaidFagment = OrderListFragment.getInstance(OrderListFragment.UNPAID_ORDER,"","user","");
        fragments.add(unPaidFagment);
        unDeliveryFagment = OrderListFragment.getInstance(OrderListFragment.UNDELIVERY,"","user","");
        fragments.add(unDeliveryFagment);
        unReceivedFagment = OrderListFragment.getInstance(OrderListFragment.UNRECIEVERED,"","user","");
        fragments.add(unReceivedFagment);
        unComemtedFagment = OrderListFragment.getInstance(OrderListFragment.UNCOMMENTED,"","user","");
        fragments.add(unComemtedFagment);

        for(int i=0;i<titles.length;i++){
            tabLayout.addTab(titles[i],i == 0);
        }

        adapter = new HomeFragmentPagerAdapter(getSupportFragmentManager(),fragments);
        viewPager.setAdapter(adapter);

        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout.getTabLayout()));
        tabLayout.setupWithViewPager(viewPager);
        (tabLayout.getTabLayout()).getTabAt(Integer.parseInt(getBundleString("position"))).select();
        viewPager.setOffscreenPageLimit(5);
        IntentFilter filter = new IntentFilter();
        filter.addAction(action);//为BroadcastReceiver指定一个action，即要监听的消息名字
        registerReceiver(mBoradcastReceiver, filter);


    }

    @Override
    protected void onDestroy() {
//        rxBus.unregister(Config.MSG_UNREAD);
        super.onDestroy();
        unregisterReceiver(mBoradcastReceiver);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Bundle bundle = intent.getExtras();
        if(bundle!=null){
            (tabLayout.getTabLayout()).getTabAt(Integer.parseInt(bundle.getString("position"))).select();
        }

    }

    @OnClick({R.id.rl_back,R.id.iv_search}) void onClick(View v){
        switch (v.getId()){
            case R.id.rl_back:
                finish();
                break;
            case R.id.iv_search:
//                Intent intent = new Intent(CustomerOrderActivity.this, CommonSearchActivity.class);
//                intent.putExtra("type", 2);
//                startActivity(intent);
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

//    public void setIndicator(TabLayout tabs, int leftDip, int rightDip) {
//        Class<?> tabLayout = tabs.getClass();
//        Field tabStrip = null;
//        try {
//            tabStrip = tabLayout.getDeclaredField("mTabStrip");
//        } catch (NoSuchFieldException e) {
//            e.printStackTrace();
//        }
//        if (tabStrip != null) {
//            tabStrip.setAccessible(true);
//        }
//        LinearLayout llTab = null;
//        try {
//            if (tabStrip != null) {
//                llTab = (LinearLayout) tabStrip.get(tabs);
//            }
//        } catch (IllegalAccessException e) {
//            e.printStackTrace();
//        }
//        int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
//        int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());
//
//        if (llTab != null) {
//            for (int i = 0; i < llTab.getChildCount(); i++) {
//                View child = llTab.getChildAt(i);
//                child.setPadding(0, 0, 0, 0);
//                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
//                params.leftMargin += left;
//                params.rightMargin += right;
//                child.setLayoutParams(params);
//                child.invalidate();
//            }
//        }
//    }
}
